/**
 * @see https://umijs.org/zh-CN/plugins/plugin-access
 * */
export default function access(initialState: { currentUser?: API.CurrentUser } | undefined) {
  const { currentUser } = initialState ?? {};
  const superAdmin = currentUser?.permissions;
  const assign = {};

  // 可优化！！！
  // 需将所有权限值先定为false
  const allAccess = {
    welcome: false,
    map: false,
    risk_list: false,
    risk_facility: false,
    hidden_risks: false,
    control: false,
    troubleshoot: false,
    rule: false,
    hidden: false,
    hidden_manage: false,
    report: false,
    monitor: false,
    things: false,
    vedio: false,
    early_warn: false,
    system_warning: false,
    case_warning: false,
    emergency: false,
    reserve_plan: false,
    resource: false,
    system: false,
    facility_type: false,
    code: false,
    permission: false,
    unit: false,
    api: false,
    log: false,
    statistics: false,
    point_info: false,
    hidden_info: false,
    troubleshoot_info: false,
    emergency_info: false,
    analysis: false,
    screen: false,
  };

  // 遍历取出路径值
  function getPerData(pers: any, res: any) {
    pers.forEach((item: any) => {
      res.push(item.path.substring(1));
      if (item.permissions && item.permissions.length) getPerData(item.permissions, res);
    });
  }

  if (superAdmin) {
    const data: never[] = [];
    getPerData(superAdmin, data);

    // 将有权限的改为true
    for (const key in allAccess) {
      data.forEach((item: any) => {
        if (item === key) {
          assign[item] = true;
        }
      });
    }
  }
  return { ...allAccess, ...assign };
}
